﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace b._5
{
    internal class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[3];     //arr数组用来储存类所需的变量

            //随机创建十个形状对象

            a[0] = 1;
            graphFactory.createGraph("Rectangle", a);
            a[0] = 3; a[1] = 2;
            graphFactory.createGraph("Square", a);
            a[0] = 5;
            graphFactory.createGraph("Triangle", a);
            a[0] = 6; a[1] = 4; a[2] = 5;
            graphFactory.createGraph("Square", a);
            a[0] = 2;
            graphFactory.createGraph("Rectangle", a);
            a[0] = 2; a[1] = 5;
            graphFactory.createGraph("Square", a);
            a[0] = 4;
            graphFactory.createGraph("Triangle", a);
            a[0] = 5; a[1] = 8; a[2] = 10;
            graphFactory.createGraph("Square", a);
            a[0] = 7;
            graphFactory.createGraph("Rectangle", a);
            a[0] = 10; a[1] = 8;
            graphFactory.createGraph("Square", a);
            Console.WriteLine("总面积为：" + graphFactory.get_total_area());
            Console.ReadLine();
        }
    }
    public interface graph
    {
        double area();
    }
    public class Square : graph
    {
        public double side;

        public Square(double s)
        {
            side = s;
        }

        public double area()
        {
            return side * side;
        }
    }
    public class Rectangle : graph
    {
        public double length;
        public double width;

        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }

        public double area()
        {
            return width * length;
        }
    }
    public class Circle : graph
    {
        public double R;

        public Circle(double r)
        {
            R = r;
        }

        public double area()
        {
            return 3.14 * R * R;
        }
    }

    public class Triangle : graph
    {
        public double S1;
        public double S2;
        public double S3;

        public Triangle(double L1, double L2, double L3)
        {
            S1 = L1;
            S2 = L2;
            S3 = L3;
        }

        public double area()
        {
            double p, S;
            p = (S1 + S2 + S3) / 2;
            S = p * (p - S1) * (p - S2) * (p - S3);
            S = Math.Sqrt(S);
            return S;
        }
    }
    public class graphFactory
    {
        public static double area_sum = 0;  //记录面积总和

        public static graph createGraph(string Name, int[] arr)
        {
            graph graph = null;
            switch (Name)
            {
                case "Square":
                    graph = new Square(arr[0]);
                    area_sum += graph.area();
                    return graph;
                case "Rectangle":
                    graph = new Rectangle(arr[0], arr[1]);
                    area_sum += graph.area();
                    return graph;
                case "Circle":
                    graph = new Circle(arr[0]);
                    area_sum += graph.area();
                    return graph;
                case "Triangle":
                    graph = new Triangle(arr[0], arr[1], arr[2]);
                    area_sum += graph.area();
                    return graph;
                default:
                    return null;
            }
        }

        public static double get_total_area()
        {
            return area_sum;
        }
    }
}
